package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetSetPhoneRandModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.SetUserPhoneModel;

public class SetPhoneActivity extends BaseActivity implements View.OnClickListener {

    EditText setphone_password, setphone_phone, setphone_rand;
    ImageView setphone_fh,iv_see_passwordphone;
    Button setphone_btn;
    TextView setphone_hqyzm;
    private int i = 60;
    private String phone,password,rand,userid;
    private Dialog mDialog;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1 == 0) {
                setphone_hqyzm.setText("点击重新获取");
                setphone_hqyzm.setClickable(true);
                i = 60;
            } else {
                setphone_hqyzm.setText(msg.arg1 + "秒后可重新发送");
                startTime();
                setphone_hqyzm.setClickable(false);
            }
        }
    };

    @Override
    void initView() {
        setLayout_file(R.layout.activity_set_phone);
        setphone_password = (EditText) findViewById(R.id.setphone_password);
        setphone_phone = (EditText) findViewById(R.id.setphone_phone);
        setphone_rand = (EditText) findViewById(R.id.setphone_rand);

        setphone_fh = (ImageView) findViewById(R.id.setphone_fh);
        iv_see_passwordphone = (ImageView)findViewById(R.id.iv_see_passwordphone);

        setphone_btn = (Button) findViewById(R.id.setphone_btn);
        setphone_hqyzm = (TextView) findViewById(R.id.setphone_hqyzm);
    }

    @Override
    void initEvent() {
        setphone_hqyzm.setOnClickListener(this);
        setphone_btn.setOnClickListener(this);
        setphone_fh.setOnClickListener(this);
        iv_see_passwordphone.setOnClickListener(this);
    }

    @Override
    void initDate() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setphone_btn:
                rand = setphone_rand.getText().toString().trim();
                userid = getid();
                password = setphone_password.getText().toString().trim();
                phone = setphone_phone.getText().toString().trim();
                //Toast.makeText(SetPhoneActivity.this,getphone(),Toast.LENGTH_SHORT).show();
                if (password.equals("")||rand.equals("")||phone.equals("")){
                    Toast.makeText(SetPhoneActivity.this,"不能为空",Toast.LENGTH_SHORT).show();
                }
                else if (!phone.equals(getphone())){
                    Toast.makeText(SetPhoneActivity.this,"号码输入和上次不一致",Toast.LENGTH_SHORT).show();
                }
                else{
            SetUserPhoneModel setUserPhoneModel = new SetUserPhoneModel();
            setUserPhoneModel.setUserPhone(userid,password,phone,rand,successBeanTListener1);
        }
        break;

        case R.id.setphone_hqyzm:
        phone = setphone_phone.getText().toString().trim();
        password = setphone_password.getText().toString().trim();
        userid = getid();
        if (!isMobileNO(phone)) {
            Toast.makeText(SetPhoneActivity.this, "手机号格式不正确", Toast.LENGTH_SHORT).show();
            break;
        } else if (password.equals("")) {
            Toast.makeText(SetPhoneActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
            break;
        } else {
            mDialog = WeiboDialogUtils.createLoadingDialog(SetPhoneActivity.this, "获取中...");
            GetSetPhoneRandModel getSetPhoneRandModel = new GetSetPhoneRandModel();
            getSetPhoneRandModel.getSetPasswordRand(userid, password, phone, successBeanTListener);
        }
        break;
        case R.id.setphone_fh:
                finish();
                break;
            case R.id.iv_see_passwordphone:
                setPasswordVisibility();
                break;

    }
}
    private void setPasswordVisibility(){
        if (iv_see_passwordphone.isSelected()) {
            iv_see_passwordphone.setSelected(false);
            setphone_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        } else {
            iv_see_passwordphone.setSelected(true);
            setphone_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
    }

    TListener<SuccessBean> successBeanTListener1 = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            String success = successBean.getSuccess();
            if (success.equals("1")){
                Toast.makeText(SetPhoneActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                finish();
            }
            else if (success.equals("2")){
                Toast.makeText(SetPhoneActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
            }
            else if (success.equals("3")){
                Toast.makeText(SetPhoneActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(SetPhoneActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(SetPhoneActivity.this, "错误", Toast.LENGTH_SHORT).show();
        }
    };

    TListener<SuccessBean> successBeanTListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            String success = successBean.getSuccess();
            if (success.equals("1")) {
                Toast.makeText(SetPhoneActivity.this, "获取验证码成功", Toast.LENGTH_SHORT).show();
                saveUserInfo();
                startTime();
                DialogThridUtils.closeDialog(mDialog);
            } else if (success.equals("2")) {
                Toast.makeText(SetPhoneActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                DialogThridUtils.closeDialog(mDialog);
            } else if (success.equals("3")) {
                Toast.makeText(SetPhoneActivity.this, "当前号码已被注册", Toast.LENGTH_SHORT).show();
                DialogThridUtils.closeDialog(mDialog);
            } else {
                Toast.makeText(SetPhoneActivity.this, "修改错误", Toast.LENGTH_SHORT).show();
                DialogThridUtils.closeDialog(mDialog);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(SetPhoneActivity.this, "错误", Toast.LENGTH_SHORT).show();
            DialogThridUtils.closeDialog(mDialog);
        }
    };

    private static boolean isMobileNO(String mobiles) {//判断是否为手机号的正则表达式
        String telRegex = "[1][3589]\\d{9}";
        if (TextUtils.isEmpty(mobiles)) return false;
        else return mobiles.matches(telRegex);
    }

    public void startTime() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                if (i > 0) {
                    i--;
                    Message message = handler.obtainMessage();
                    message.arg1 = i;
                    handler.sendMessage(message);
                }
            }
        };
        timer.schedule(task, 1000);
    }

    private void saveUserInfo() {//存储用户信息
        SharedPreferences.Editor editor = getSharedPreferences("userInfo", MODE_PRIVATE).edit();
        editor.putString("phone",phone);
        editor.commit();
    }
}